<?php 
session_start();
$page = 'find-bus';
include('config.php');
if(isset($_POST['submit'])){
	$bus = $_POST['bus'];
	$lat = $_POST['lat'];
	$long = $_POST['long'];
	if($lat=="")
		$lat = 40.443607;
	if($long=="")
		$long = -79.9451341;
		
	echo $bus . "  " . $lat . "  " . $long;
	$query = 'SELECT stop_id, stop_name, ';
	$query .= ' truncate((degrees(acos(';
    $query .= '    sin(radians(stop_lat))';
    $query .= '    * sin( radians(' . $lat .'))';
    $query .= '    + cos(radians(stop_lat))';
    $query .= '    * cos( radians('.$lat.'))';
    $query .= '    * cos( radians(stop_lon - ('.$long.')) )';
    $query .= '    ) ) * 69.09),1) as distance ';
	$query .= ' FROM stops';
	$query .= ' HAVING distance < 0.8';
	$query .= ' ORDER BY distance ASC;';
	
	//echo "<br>". $query . "<br>";
	
	$stmt = $conn->prepare($query);
	$stmt->execute();
	
	$results = $stmt->fetchAll();
	$stopIds = array();
//	echo "<br>";
	foreach($results as $stop){
		//echo $stop['stop_id'] . " => " .$stop['stop_name'] . "<br>";
		$stopIds[] = $stop['stop_id'];
	}
	
	//$now = '14:00:00'; //
	//$time_offset =525;
	date_default_timezone_set('America/New_York');
	$now = date('H:i:s', time());
	//$nowIncr = '14:30:00'; //date('H:i:s', time()*2+ 3600);
	$nowIncr = date('H:i:s', time() + 1800);
	
	echo "<br>now = " . $now;
	echo "<br> next = " . $nowIncr;
	
	$q = Doctrine_Query::create()
			->select('*')
			->from('StopTimes st, st.Stops')
			->whereIn('st.stop_id', $stopIds)
			->andWhere('st.arrival_time BETWEEN ? AND ?', array($now, $nowIncr) );
			
	$q->getSqlQuery();
	$r = $q->execute();
	
	//echo "<br>stopIds count = " . $r->count();
	
	$stop_times_ids = array();
	foreach($r as $st){
		//echo "<br>". $st->Stops->stop_name." => " . $st->arrival_time;
		$stop_times_ids[] = $st->stop_time_id;
	}
	
	//print_r($stop_times_ids);
	//uncomment this to filter by BUS Number 
	//echo "<Br><Br>======= Next query =======<Br>";
	
	$q = Doctrine_Query::create()
			->select('rts.route_id, st.stop_id, st.stop_time_id, stp.stop_id, st.arrival_time, stp.stop_name, tps.trip_id')
			//->from('Routes rts, rts.Trips tps, tps.StopTimes st, st.Stops stp')
			//->from('StopTimes st,  st.Trips tps, tps.Routes rts,  st.Stops stp')
			->from('Stops stp, stp.StopTimes st,  st.Trips tps, tps.Routes rts')
			->where('rts.route_short_name LIKE ?', $bus)
			->andWhereIn('st.stop_time_id',$stop_times_ids)
			->orderBy('st.arrival_time ASC');
			//->andWhere('tps.route_id = rts.route_id')
			//->andWhere('st.trip_id = tps.trip_id');
			//->andWhere();
	$u = $q->execute(array(), Doctrine::HYDRATE_ARRAY);
	
	//print_r($u);
	/*
	foreach($u as $result){
		echo "<br/><br/>===" . $result['stop_name']. "===<br/>";
		foreach($result['StopTimes'] as $stops){
			echo date('g:ia',strtotime($stops['arrival_time'])) . "<br>";
		} 
	}
	*/
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<meta name="viewport" content="width=320, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Bus Results: <?php echo @$_POST['bus'];?></title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAA1Kb4h1tNxUp5QKPg18z2vhT3hW7-_cfanIUGdvUNbalm8ZIe4hT-E0yHzCliJKxNBQ1r147vO8Pqiw" type="text/javascript"></script>
<script src="http://www.google.com/jsapi?key=ABQIAAAA1Kb4h1tNxUp5QKPg18z2vhT3hW7-_cfanIUGdvUNbalm8ZIe4hT-E0yHzCliJKxNBQ1r147vO8Pqiw" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
  
</script>  
</head>
<body>
<div id="body-wrapper">
	<div id="header"><?php include('menu.php');?></div>
	<div id="Content">
	<?php foreach($u as $result):?>
		<div class="row trip"  style="margin-top:5px;"> 
			<div class="blue bold"><?php echo ucwords(strtolower($result['stop_name']));?></div><br/>
			<?php foreach($result['StopTimes'] as $stops):?>
				<img src="images/bus.png" alt="bus"/> <?php echo date('g:ia',strtotime($stops['arrival_time'])) . "<br>";?>
			<?php endforeach;?>
		</div>
	<?php endforeach;?>
		<div class="clear"></div>
		<br/>
		<a href="#">View More &gt;&gt;</a>
		<br/><br/> &nbsp;
	</div>
</div>
</body>
</html>